#! /bin/bash

my_path="/opt/rgrub"
rm -rf $my_path/grubenv
grub-editenv list > $my_path/grubenv
grub-editenv /boot/grub/grubenv set which=
ret=`grep 'which=rgrub' $my_path/grubenv`
if [[ ! -n "$ret" ]]; then
	exit 0
fi

ip=`cat $my_path/ip_port | sed -n '1p'`
port=`cat $my_path/ip_port | sed -n '2p'`
mac=`ifconfig |grep HWaddr |awk '{print $5}'`
header="--header=RGrub: $mac"
outfile="--output-document=$my_path/rgrub_set"
url="http://$ip:$port/rgrub"
upload="--post-file=$my_path/rgrub.cfg"
echo ${url} ${header} ${upload} ${outfile} > $my_path/wgetlog
/usr/bin/wget ${url} "${header}" "${upload}" "${outfile}" --retry-connrefused &>> $my_path/wgetlog

next=`cat $my_path/rgrub_set`
echo "next=$next" >> $my_path/wgetlog
grub-reboot "$next"
reboot
